feat: OAuth 2.1 resource-server support (MCP authorization)#20
Open
VinciGit00 wants to merge 1 commit into
Open
feat: OAuth 2.1 resource-server support (MCP authorization)#20VinciGit00 wants to merge 1 commit into
VinciGit00 wants to merge 1 commit into
Conversation
Add optional OAuth 2.1 (MCP authorization) so remote clients can sign in via the ScrapeGraphAI web app instead of passing an API key. When MCP_OAUTH_AUTH_SERVER is set, the server runs as an OAuth resource server (FastMCP RemoteAuthProvider): it verifies opaque better-auth access tokens against the AS session endpoint and forwards the bearer to the ScrapeGraphAI API, which resolves the user. Falls back to the existing API-key auth when OAuth is disabled (stdio / Smithery / legacy remote). - BetterAuthTokenVerifier + _build_auth_provider() (RemoteAuthProvider) - get_credentials()/make_client(): bearer-first with API-key fallback - all tool handlers build their client via make_client(ctx) - env: MCP_OAUTH_AUTH_SERVER, MCP_PUBLIC_URL, MCP_OAUTH_VERIFY_URL - update .agent docs, render.yaml and .env.example Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds optional OAuth 2.1 (MCP authorization) so remote MCP clients can sign in through the ScrapeGraphAI web app instead of passing an API key. The server becomes an OAuth resource server; the API-key path remains as a fallback.
Backend counterpart (authorization server + API bearer validation): ScrapeGraphAI/sgai-stack#796
How it works
MCP_OAUTH_AUTH_SERVERis set, the server registers a FastMCPRemoteAuthProviderand exposes protected-resource metadata at/.well-known/oauth-protected-resource.BetterAuthTokenVerifiervalidates the incoming Bearer token by calling the authorization server's session endpoint (/api/auth/mcp/get-session) — better-auth issues opaque tokens (not JWTs), so validity is confirmed server-to-server.Authorization: Bearer …, which resolves the user.get_credentials()/make_client()prefer the OAuth bearer and fall back to the legacySGAI-APIKEY(fromX-API-Keyheader, env, or--config) for stdio / Smithery / existing remote users.Changes
BetterAuthTokenVerifier+_build_auth_provider()get_credentials()/make_client(); all tool handlers now build the client viamake_client(ctx)MCP_OAUTH_AUTH_SERVER,MCP_PUBLIC_URL,MCP_OAUTH_VERIFY_URL.env.example,render.yaml, and.agent/docs updatedTesting
Verified end-to-end locally against the better-auth authorization server (
apps/web) and the ScrapeGraphAI API (apps/api): login flow (discovery → DCR → authorize → token), token verification, authenticated MCP session, and 16/17 tools working through the full OAuth chain (schema404s only because that route isn't implemented in the backend). With OAuth disabled the server keeps the existing API-key behaviour.🤖 Generated with Claude Code